package com.oracle.lingshangejia.activity.special;

import android.app.DatePickerDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.special.ET36ArchivesExtendableListAdapter;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.bean.ParentsBean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.dialog.LoadingViewDialog;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//儿童3到6岁
public class ErTong36ArchivesActivity extends AppCompatActivity {
    private ExpandableListView expandableListView;
    private ET36ArchivesExtendableListAdapter archivesExtendableListAdapter;


    private static int[] childItems = {R.layout.et_36_archives_child_item_0, R.layout.et_36_archives_child_item_1, R.layout.et_36_archives_child_item_2, R.layout.et_36_archives_child_item_3
                , R.layout.et_36_archives_child_item_4};
    // 用于存放折叠的视图
    private View[] viewss  = new View[childItems.length];

    private Spinner ageSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.et_36_add_archives_activity);
         initBasic();
        ageSpinner = findViewById(R.id.threenewborn_ageOfTheMoon);


        expandableListView = findViewById(R.id.expend_list);
        expandableListView.setGroupIndicator(null);
        String[] archivesStr = getResources().getStringArray(R.array.et_36_archives);
        archivesExtendableListAdapter = new ET36ArchivesExtendableListAdapter(archivesStr);
        for(int i = 0; i < viewss.length; i++) {
            viewss[i] = LayoutInflater.from(expandableListView.getContext()).inflate(childItems[i], expandableListView, false);
        }
        archivesExtendableListAdapter.setConvertViews(viewss);
        expandableListView.setAdapter(archivesExtendableListAdapter);
        // 添加末尾提交按钮
        View foot = LayoutInflater.from(this).inflate(R.layout.archives_foot_submit, null);
        expandableListView.addFooterView(foot);

//        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
//            @Override
//            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//                return checkBasicInfoIsError();
//            }
//        });
//
//        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
//            @Override
//            public void onGroupExpand(int groupPosition) {
//                int count = expandableListView.getExpandableListAdapter().getGroupCount();
//                for (int i = 0; i < count; i++) {
//                    if (i != groupPosition) {
//                        expandableListView.collapseGroup(i);
//                    }
//                }
//            }
//        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                // 页面没加载时不折叠
                if(viewss == null)
                    return;
                int count = expandableListView.getExpandableListAdapter().getGroupCount();
                for (int i = 0; i < count; i++) {
                    if (i != groupPosition) {
                        expandableListView.collapseGroup(i);
                    }
                }

            }
        });

        ageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //取得被选中的列表项的文字
                String age = parent.getItemAtPosition(position).toString();
                for(View v:viewss) {
                    System.out.println("视图：" + v);
                }
                TableRow eyesight = viewss[2].findViewById(R.id.eyesight);
                TableRow hearing = viewss[2].findViewById(R.id.hearing);
                TableLayout Developmentalassessment1 = viewss[2].findViewById(R.id.threenewborn_Developmentalassessment1);
                TableLayout Developmentalassessment2 = viewss[2].findViewById(R.id.threenewborn_Developmentalassessment2);
                TableLayout Developmentalassessment3 = viewss[2].findViewById(R.id.threenewborn_Developmentalassessment3);
                TableLayout Developmentalassessment4 = viewss[2].findViewById(R.id.threenewborn_Developmentalassessment4);

                if("1 三岁".equals(age)) {
                    hearing.setVisibility(View.VISIBLE);
                    Developmentalassessment1.setVisibility(View.VISIBLE);

                    eyesight.setVisibility(View.GONE);
                    Developmentalassessment2.setVisibility(View.GONE);
                    Developmentalassessment3.setVisibility(View.GONE);
                    Developmentalassessment4.setVisibility(View.GONE);
                }
                if("2 四岁".equals(age)) {
                    eyesight.setVisibility(View.VISIBLE);
                    Developmentalassessment2.setVisibility(View.VISIBLE);

                    hearing.setVisibility(View.GONE);
                    Developmentalassessment1.setVisibility(View.GONE);
                    Developmentalassessment3.setVisibility(View.GONE);
                    Developmentalassessment4.setVisibility(View.GONE);
                }
                if("3 五岁".equals(age)) {
                    eyesight.setVisibility(View.VISIBLE);
                    Developmentalassessment3.setVisibility(View.VISIBLE);

                    hearing.setVisibility(View.GONE);
                    Developmentalassessment2.setVisibility(View.GONE);
                    Developmentalassessment1.setVisibility(View.GONE);
                    Developmentalassessment4.setVisibility(View.GONE);
                }
                if("4 六岁".equals(age)) {
                    eyesight.setVisibility(View.VISIBLE);
                    Developmentalassessment4.setVisibility(View.VISIBLE);

                    hearing.setVisibility(View.GONE);
                    Developmentalassessment2.setVisibility(View.GONE);
                    Developmentalassessment3.setVisibility(View.GONE);
                    Developmentalassessment1.setVisibility(View.GONE);
                }

                // 获取数据后只留第一个展开，其余关闭
                int count = expandableListView.getExpandableListAdapter().getGroupCount();
                for (int i = 0; i < count; i++) {
                    if(i == 0) {
                        expandableListView.expandGroup(i);
                    } else {
                        expandableListView.collapseGroup(i);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        // 页面加载完展开所有数据，用于获取视图数据
        int count = expandableListView.getExpandableListAdapter().getGroupCount();
        for (int i = 0; i < count; i++) {
            expandableListView.expandGroup(i);
        }

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                View[] views = ((ET36ArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
                View view = views[groupPosition];
                if (view == null) {
                    return;
                }
                Map<String, String> values = null;
                switch (groupPosition) {
                    case 0:
                        values = getBasicInfoValues();
                        break;
                    case 1:
                        values = getSixMouthsHealthManagementofChineseMedicineValues(view);
                        break;
                    case 2:
                        values = essentialInformation2(view);
                        break;
                    case 3:
                        values = essentialInformation3(view);
                        break;
                    case 4:
                        values = essentialInformation4(view);
                        break;


                }


            }
        });

        expandableListView.expandGroup(0);

    }
    /**
     * 检查用户名、身份证号和本人联系电话是否正确填写
     *
     * @return 数据不正确返回true，正确返回false
     */
    private boolean checkBasicInfoIsError() {
        Map<String, String> basicInforValues = getBasicInfoValues();
        String xm = (String) basicInforValues.get("xm");
        // 姓名非空验证
        if (xm == null || xm.length() == 0) {
            Toast.makeText(getApplicationContext(), "姓名必须填写", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }

    /**
     * 获取基本信息数据
     */
    private Map<String, String> getBasicInfoValues() {
        View[] views = ((ET36ArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
        View view = views[0];
        Map<String, String> params = new HashMap<>();
        // 姓名
        String name = "";
        //居民ID
        String residentID="";
        //父亲姓名
        String fathername="";
        //父亲电话
        String fahterphone="";
        //父亲出生日期
        String fatherbirthday="";
        //父亲职业
        String fatherprofession="";
        //母亲姓名
        String mothername="";
        //母亲电话
        String mohterphone="";
        //母亲出生日期
        String motherbirthday="";
        //母亲职业
        String motherprofession="";
        //医生ID
        String doctorID="";
        //月龄
        String age="";


      //  name = ((EditText) view.findViewById(R.id.threenewborn_username)).getText().toString().trim();
       // residentID=((EditText) view.findViewById(R.id.threenewborn_residentID)).getText().toString().trim();
        fathername=((EditText) view.findViewById(R.id.threenewborn_fathername)).getText().toString().trim();
        fatherprofession=((EditText) view.findViewById(R.id.threenewborn_fatherprofession)).getText().toString().trim();
        fahterphone=((EditText) view.findViewById(R.id.threenewborn_fatherphone)).getText().toString().trim();
        fatherbirthday = ((TextView) view.findViewById(R.id.threenewborn_fatherbirthday)).getText().toString().trim();
        mothername=((EditText) view.findViewById(R.id.threenewborn_mothername)).getText().toString().trim();
        motherprofession=((EditText) view.findViewById(R.id.threenewborn_motherprofession)).getText().toString().trim();
        mohterphone=((EditText) view.findViewById(R.id.threenewborn_motherphone)).getText().toString().trim();
        motherbirthday = ((TextView) view.findViewById(R.id.threenewborn_motherbirthday)).getText().toString().trim();
      //  doctorID=((EditText) view.findViewById(R.id.threenewborn_doctorID)).getText().toString().trim();
        age = ((Spinner) findViewById(R.id.threenewborn_ageOfTheMoon)).getSelectedItem().toString().trim();
        age = age.substring(0, age.indexOf(" "));
     //   params.put("xm", name);
     //   params.put("grjbxx_id", residentID);
        params.put("fxm", fathername);//父亲姓名
        params.put("fzy", fatherprofession);//职业
        params.put("fdh", fahterphone);//电话
        params.put("fcsrq", fatherbirthday);//出生
        params.put("mxm", mothername);//姓名
        params.put("mzy", motherprofession);//职业
        params.put("mdh", mohterphone);//电话
        params.put("mcsrq", motherbirthday);//出生
     //   params.put("create_by", doctorID);
        params.put("yl", age);  //年龄
        mValues.putAll(params);
        return params;
    }

    /**
     * 第一页
     */
    private Map<String, String> getSixMouthsHealthManagementofChineseMedicineValues(View view) {
        Map<String, String> params = new HashMap<>();
        // 随访日期
        String Followupdate3 = "";
        //体重
        String weight3 = "";
        //身高
        String height3="";
        // 体重/身高
        String weightheight3="";
        //体格发育评价
        String growth3="";

        weight3 = ((Spinner) view.findViewById(R.id.threenewborn_weight3)).getSelectedItem().toString().trim();
        weight3 = weight3.substring(0,weight3.indexOf(" "));
        //Followupdate3 = ((TextView) view.findViewById(R.id.threenewborn_Followupdate3)).getText().toString().trim();
        height3 = ((Spinner) view.findViewById(R.id.threenewborn_height3)).getSelectedItem().toString().trim();
        height3 = height3.substring(0,height3.indexOf(" "));
        weightheight3 = ((Spinner) view.findViewById(R.id.threenewborn_weightheight3)).getSelectedItem().toString().trim();
        weightheight3 = weightheight3.substring(0,weightheight3.indexOf(" "));
        growth3 = ((Spinner) view.findViewById(R.id.threenewborn_growth3)).getSelectedItem().toString().trim();
        growth3 = growth3.substring(0,growth3.indexOf(" "));
       // params.put("sfrq", Followupdate3);
        params.put("xseTz", weight3);//体重
        params.put("xseSz", height3);//身高
        params.put("xseSgTz",weightheight3);///体重身高
        params.put("xseTgfypj",growth3);///体格发育
        mValues.putAll(params);
        return params;
    }
    /*
     * 第二页
     */
    private Map<String, String> essentialInformation2(View view) {
        Map<String, String> params = new HashMap<>();
        //视力
        String eyesight3 = "";
        // 听力
        String hearing3 = "";
        // 牙数
        String tooth3 = "";
        // 殴齿数
        String tooth33 = "";
        //胸部
        String chest3 = "";
        // 腹部
        String abdomen3 = "";
        // 其他
        String else3 = "";
        // 血红蛋白
        String hemoglobin3 = "";
        //发育评估
        String Developmentalassessment3="";

        eyesight3 = ((EditText) view.findViewById(R.id.threenewborn_eyesight3)).getText().toString().trim();
        hearing3 = ((Spinner) view.findViewById(R.id.threenewborn_hearing3)).getSelectedItem().toString().trim();
        hearing3 = hearing3.substring(0,hearing3.indexOf(" "));
        tooth3 = ((EditText) view.findViewById(R.id.threenewborn_tooth3)).getText().toString().trim();
        tooth33= ((EditText) view.findViewById(R.id.threenewborn_tooth33)).getText().toString().trim();
        chest3 = ((Spinner) view.findViewById(R.id.threenewborn_chest3)).getSelectedItem().toString().trim();
        chest3 =chest3.substring(0,chest3.indexOf(" "));
        abdomen3 = ((Spinner) view.findViewById(R.id.threenewborn_abdomen3)).getSelectedItem().toString().trim();
        abdomen3 = abdomen3.substring(0,abdomen3.indexOf(" "));
        else3 = ((EditText) view.findViewById(R.id.threenewborn_else3)).getText().toString().trim();
        hemoglobin3 = ((EditText) view.findViewById(R.id.threenewborn_hemoglobin3)).getText().toString().trim();

        View[] Developmentalassessment3Views = getViews(view.findViewById(R.id.threenewborn_Developmentalassessment1));
        for (int i = 0; i < Developmentalassessment3Views.length; i++) {
            if (Developmentalassessment3Views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) Developmentalassessment3Views[i];
                if (cb.isChecked()) {
                    String s = cb.getText().toString().trim();
                    s = s.substring(0, s.indexOf(" "));
                    MyLogger.i(s);
                    Developmentalassessment3 += s;
                    Developmentalassessment3 += ",";
                }
            }
        }

        if (Developmentalassessment3.length() != 0) {
            Developmentalassessment3 = Developmentalassessment3.substring(0, Developmentalassessment3.length() - 1);
        }
        String Developmentalassessment4="";
        View[] Developmentalassessment4Views = getViews(view.findViewById(R.id.threenewborn_Developmentalassessment2));
        for (int i = 0; i < Developmentalassessment4Views.length; i++) {
            if (Developmentalassessment4Views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) Developmentalassessment4Views[i];
                if (cb.isChecked()) {
                    String s = cb.getText().toString().trim();
                    s = s.substring(0, s.indexOf(" "));
                    MyLogger.i(s);
                    Developmentalassessment4 += s;
                    Developmentalassessment4 += ",";
                }
            }
        }

        if (Developmentalassessment4.length() != 0) {
            Developmentalassessment4 = Developmentalassessment4.substring(0, Developmentalassessment4.length() - 1);
        }
        String Developmentalassessment5="";


        View[] Developmentalassessment5Views = getViews(view.findViewById(R.id.threenewborn_Developmentalassessment3));
        for (int i = 0; i < Developmentalassessment5Views.length; i++) {
            if (Developmentalassessment5Views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) Developmentalassessment5Views[i];
                if (cb.isChecked()) {
                    String s = cb.getText().toString().trim();
                    s = s.substring(0, s.indexOf(" "));
                    Developmentalassessment5 += s;
                    Developmentalassessment5 += ",";
                }
            }
        }

        if (Developmentalassessment5.length() != 0) {
            Developmentalassessment5 = Developmentalassessment5.substring(0, Developmentalassessment5.length() - 1);
        }
        String Developmentalassessment6="";


        View[] Developmentalassessment6Views = getViews(view.findViewById(R.id.threenewborn_Developmentalassessment4));
        for (int i = 0; i < Developmentalassessment6Views.length; i++) {
            if (Developmentalassessment6Views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) Developmentalassessment6Views[i];
                if (cb.isChecked()) {
                    String s = cb.getText().toString().trim();
                    s = s.substring(0, s.indexOf(" "));
                    Developmentalassessment6 += s;
                    Developmentalassessment6 += ",";
                }
            }
        }


        if (Developmentalassessment6.length() != 0) {
            Developmentalassessment6 = Developmentalassessment6.substring(0, Developmentalassessment6.length() - 1);
        }
        if (!TextUtils.isEmpty(Developmentalassessment6)) {
            params.put("fypg", Developmentalassessment6); //发育评估
        }

        if (!TextUtils.isEmpty(Developmentalassessment5)) {
            params.put("fypg", Developmentalassessment5); //发育评估
        }

        if (!TextUtils.isEmpty(Developmentalassessment4)) {
            params.put("fypg", Developmentalassessment4); //发育评估
        }

        if (!TextUtils.isEmpty(Developmentalassessment3)) {
            params.put("fypg", Developmentalassessment3); //发育评估
        }
        MyLogger.i( Developmentalassessment6);
        MyLogger.i( Developmentalassessment4);
        MyLogger.i( Developmentalassessment3);
        MyLogger.i( Developmentalassessment5);

        params.put("xseSl",  eyesight3);//视力
        params.put("xseTl", hearing3);//听力
        params.put("xseZcs", tooth3);//牙颗数
        params.put("xseCys", tooth33);//出牙数
        params.put("xseXb", chest3);//胸部
        params.put("xseFb", abdomen3);//腹部
        params.put("xseXhdbz", hemoglobin3);//血红蛋白值
        params.put("xseQt", else3);//其他
        mValues.putAll(params);
        MyLogger.i((new JSONObject(params).toString())+"");
        return params;
    }

    // 第三页，出生信息
    private Map<String, String> essentialInformation3(View view) {
        Map<String, String> params = new HashMap<>();
        //两次随访间患病情况
        String familialDiseaseChildren = "";
        String DiseasepneumoniaTime = "";
        String DiseasediarrheaTime="";
        String DiseasetraumaTime="";
        String other33="";


        if (view != null) {
            View[] familialDiseaseChildrenViews = getViews(view.findViewById(R.id.threenewborn_visit3));
            for (int i = 0; i < familialDiseaseChildrenViews.length; i++) {
                if (familialDiseaseChildrenViews[i] instanceof CheckBox) {
                    CheckBox cb = (CheckBox) familialDiseaseChildrenViews[i];
                    if (cb.isChecked()) {
                        String s = cb.getText().toString().trim();
                        s = s.substring(0, s.indexOf(" "));
                        familialDiseaseChildren += s;
                        familialDiseaseChildren += ",";
                    }
                }
            }
            if (familialDiseaseChildren.length() != 0) {
                familialDiseaseChildren = familialDiseaseChildren.substring(0, familialDiseaseChildren.length() - 1);
            }


            other33 = ((EditText) view.findViewById(R.id.threenewborn_other33)).getText().toString().trim();
        }
        DiseasepneumoniaTime=((EditText) view.findViewById(R.id.threenewborn_pneumonianumber3)).getText().toString().trim();
        DiseasediarrheaTime=((EditText) view.findViewById(R.id.threenewborn_diarrheanumber3)).getText().toString().trim();
        DiseasetraumaTime=((EditText) view.findViewById(R.id.threenewborn_traumanumber3)).getText().toString().trim();

        //转诊建议

        String genetic = "";
        String reason3 = "";
        String institutions3="";
        if (view != null) {
            RadioButton none = view.findViewById(R.id.threenewborn_null3);
            RadioButton all = view.findViewById(R.id.threenewborn_genetic_disease_all3);
            if (none.isChecked()) {
                String s = none.getText().toString().trim();
                s = s.substring(0, s.indexOf(" "));
                genetic = s;
            } else {
                String s = all.getText().toString().trim();
                if (s.length() > 0)
                    s = s.substring(0, s.indexOf(" "));
                genetic = s;
            }
            reason3 = ((EditText) view.findViewById(R.id.threenewborn_reason3)).getText().toString().trim();
            institutions3 = ((EditText) view.findViewById(R.id.threenewborn_institutions3)).getText().toString().trim();
        }


        params.put("lcsfjhzqk",familialDiseaseChildren);//两次随访间患者患病情况
        params.put("lcsfjhzqkCs2",DiseasepneumoniaTime);//肺炎次数
        params.put("lcsfjhzqkCs3",DiseasediarrheaTime);//腹泻次数
        params.put("lcsfjhzqkCs4",DiseasetraumaTime);//外伤次数
        params.put("lcsfjhzqkQt",other33);//两次随访间患者患病情况_其他
        params.put("zzjy",genetic);//建议
        params.put("zzjyYy",reason3);//原因
        params.put("zzjyJgjks",institutions3);//科室
        mValues.putAll(params);
        MyLogger.i((new JSONObject(params).toString())+"");
        return params;
    }
    /**
     * 第四页，新生儿身体健康
     */
    private Map<String, String> essentialInformation4(View view) {
        Map<String, String> params = new HashMap<>();

        // 下次随访日期
        String Followupdate33="";
        // 医生签名
        String signature3="";
        //指导建议
        String suggestions3="";
        // 其他指导建议
        String payother3="";

        View[] suggestions3Views = getViews(view.findViewById(R.id.threenewborn_suggestions3));
        for (int i = 0; i < suggestions3Views.length; i++) {
            if (suggestions3Views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox)suggestions3Views[i];
                if (cb.isChecked()) {
                    String s = cb.getText().toString().trim();
                    s = s.substring(0, s.indexOf(" "));
                    suggestions3 += s;
                    suggestions3 += ",";
                }
            }
        }
        if (suggestions3.length() != 0) {
            suggestions3 =suggestions3.substring(0,suggestions3.length() - 1);
        }
        payother3 = ((EditText) view.findViewById(R.id.threenewborn_pay_othertext3)).getText().toString().trim();

        Followupdate33 = ((TextView) view.findViewById(R.id.threenewborn_Followupdate33)).getText().toString().trim();
        signature3 = ((EditText) view.findViewById(R.id.threenewborn_signature3)).getText().toString().trim();
        params.put("scsfsj", Followupdate33);//下次随访
        params.put("sfysqm", signature3);//医生签名
        params.put("zd",suggestions3);//建议指导
        params.put("zdQt",payother3);//其他
         mValues.putAll(params);
         MyLogger.i((new JSONObject(params).toString())+"");
        return params;
    }

    /**
     * Toolbar的事件---返回
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 点击日期选择框弹出DatePickerDialog
     */
    private Calendar calendar;
    private DatePickerDialog dialog;

    public void onTimeViewClick(final View v) {
        calendar = Calendar.getInstance();
        dialog = new DatePickerDialog(ErTong36ArchivesActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        TextView tv = (TextView) v;
                        String month;
                        String day;
                        if (monthOfYear <9){
                            month = "0"+(monthOfYear+1);
                        }else {
                            month = ""+(monthOfYear+1);
                        }
                        if (dayOfMonth <10){
                            day = "0"+dayOfMonth;
                        }else {
                            day = dayOfMonth + "";
                        }
                        tv.setText(year + "-" + month  + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar
                .get(Calendar.MONTH), calendar
                .get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }


    //第一//
    /**
     * "转诊建议"按钮点击事件处理
     */
    public void onEdit33(View view) {
        EditText ed1 = findViewById(R.id.threenewborn_reason3);
        EditText ed2 = findViewById(R.id.threenewborn_institutions3);
        RadioButton rb = (RadioButton) view;
        if (view.getId() == R.id.threenewborn_null3) {
            ed1.setText("");
            ed1.setEnabled(false);
            ed2.setText("");
            ed2.setEnabled(false);
        } else if (rb.isChecked()) {
            ed1.setEnabled(true);
            ed2.setEnabled(true);
        }
    }

    /**
     * 发育评估按钮点击事件
     */
    public void onAllergyClick33(View view) {
        View[] views = getViews(findViewById(R.id.threenewborn_Developmentalassessment3));
        boolean flag = false;
        for (int i = 0; i < views.length; i++) {
            if (views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) views[i];
                if (cb.isChecked()) {
                    flag = true;
                }
            }
        }
    }



    /**
     * 两次随访间患病情况按钮点击事件
     */
    public void onDiseaseClick33(View view) {
        // 无
        CheckBox none = findViewById(R.id.threenewborn_none3);
        // 肺炎
        CheckBox pneumonia = findViewById(R.id.threenewborn_pneumonia3);
        EditText pneumoniaTime = findViewById(R.id.threenewborn_pneumonianumber3);
        // 腹泻
        CheckBox diarrhea = findViewById(R.id.threenewborn_diarrhea3);
        EditText diarrheaTime = findViewById(R.id.threenewborn_diarrheanumber3);
        // 外伤
        CheckBox trauma = findViewById(R.id.threenewborn_trauma3);
        EditText traumaTime = findViewById(R.id.threenewborn_traumanumber3);
        // 其它
        CheckBox other = findViewById(R.id.threenewborn_other3);
        TextView otherIll = findViewById(R.id.threenewborn_other33);
        switch (view.getId()) {
            case R.id.threenewborn_none3:
                if (none.isChecked()) {
                    // 肺炎
                    pneumonia.setChecked(false);
                    pneumoniaTime.setText("");
                    pneumoniaTime.setEnabled(false);
                    // 腹泻
                    diarrhea.setChecked(false);
                    diarrheaTime.setText("");
                    diarrheaTime.setEnabled(false);
                    // 外伤
                    trauma.setChecked(false);
                    traumaTime.setText("");
                    traumaTime.setEnabled(false);
                    // 其它
                    other.setChecked(false);
                    otherIll.setHint("");
                    otherIll.setEnabled(false);
                } else {
                    none.setChecked(true);
                }
                break;
            case R.id.threenewborn_pneumonia3:
                none.setChecked(false);
                if (pneumonia.isChecked()) {
                    pneumoniaTime.setEnabled(true);
                    pneumoniaTime.setFocusable(true);
                    pneumoniaTime.setFocusableInTouchMode(true);
                    pneumoniaTime.requestFocus();
                } else {
                    pneumoniaTime.setEnabled(false);
                    pneumoniaTime.setText("");
                    if (!pneumonia.isChecked() && !diarrhea.isChecked()
                            && !trauma.isChecked() && !other.isChecked()) {
                        none.setChecked(true);
                    }
                }
                break;
            case R.id.threenewborn_diarrhea3:
                none.setChecked(false);
                if (diarrhea.isChecked()) {
                    diarrheaTime.setEnabled(true);
                    diarrheaTime.setFocusable(true);
                    diarrheaTime.setFocusableInTouchMode(true);
                    diarrheaTime.requestFocus();
                } else {
                    diarrheaTime.setEnabled(false);
                    diarrheaTime.setText("");
                    if (!pneumonia.isChecked() && !diarrhea.isChecked()
                            && !trauma.isChecked() && !other.isChecked()) {
                        none.setChecked(true);
                    }
                }
                break;
            case R.id.threenewborn_trauma3:
                none.setChecked(false);
                if (trauma.isChecked()) {
                    traumaTime.setEnabled(true);
                    traumaTime.setFocusable(true);
                    traumaTime.setFocusableInTouchMode(true);
                    traumaTime.requestFocus();
                } else {
                    traumaTime.setEnabled(false);
                    traumaTime.setText("");
                    if (!pneumonia.isChecked() && !diarrhea.isChecked()
                            && !trauma.isChecked() && !other.isChecked()) {
                        none.setChecked(true);
                    }
                }
                break;
            case R.id.threenewborn_other3:
                none.setChecked(false);
                if (other.isChecked()) {
                    otherIll.setEnabled(true);
                    otherIll.setFocusable(true);
                    otherIll.setFocusableInTouchMode(true);
                    otherIll.requestFocus();
                } else {
                    otherIll.setEnabled(false);
                    otherIll.setText("");
                    if (!pneumonia.isChecked() && !diarrhea.isChecked()
                            && !trauma.isChecked() && !other.isChecked()) {
                        none.setChecked(true);
                    }
                }
                break;
        }
    }

    /**
     * 指导建议按钮点击事件
     */
    public void onPayOtherClick33(View view) {
        CheckBox cb = (CheckBox) view;
        EditText et = findViewById(R.id.threenewborn_pay_othertext3);
        if (cb.isChecked()) {
            et.setEnabled(true);
            et.setFocusable(true);
            et.setFocusableInTouchMode(true);
            et.requestFocus();
        } else {
            et.setText("");
            et.setEnabled(false);
        }
    }

//第二//


    /**
     * 发育评估按钮点击事件
     */
    public void onAllergyClick44(View view) {
        View[] views = getViews(findViewById(R.id.threenewborn_Developmentalassessment4));
        boolean flag = false;
        for (int i = 0; i < views.length; i++) {
            if (views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) views[i];
                if (cb.isChecked()) {
                    flag = true;
                }
            }
        }
    }






//第三//



    /**
     * 发育评估按钮点击事件
     */
    public void onAllergyClick55(View view) {
        View[] views = getViews(findViewById(R.id.threenewborn_Developmentalassessment3));
        boolean flag = false;
        for (int i = 0; i < views.length; i++) {
            if (views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) views[i];
                if (cb.isChecked()) {
                    flag = true;
                }
            }
        }
    }







//第四//



    /**
     * 发育评估按钮点击事件
     */
    public void onAllergyClick66(View view) {
        View[] views = getViews(findViewById(R.id.threenewborn_Developmentalassessment4));
        boolean flag = false;
        for (int i = 0; i < views.length; i++) {
            if (views[i] instanceof CheckBox) {
                CheckBox cb = (CheckBox) views[i];
                if (cb.isChecked()) {
                    flag = true;
                }
            }
        }
    }

    /**
     * 获取子视图
     */
    private View[] views;

    private View[] getViews(View view) {
        views = new View[0];
        parseView(view);
        return views;
    }

    private void parseView(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) view;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View v = vg.getChildAt(i);
                parseView(v);
            }
        } else {
            views = Arrays.copyOf(views, views.length + 1);
            views[views.length - 1] = view;
        }
    }

    /**
     * 提交数据
     */
    public void onClickSubmit(View view) {
        showLoading();
        int count = expandableListView.getExpandableListAdapter().getGroupCount();
        for (int i = 0; i < count; i++) {
            expandableListView.collapseGroup(i);
        }
        Observable.create(new ObservableOnSubscribe<DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<DataBean> emitter) throws Exception {
                JSONObject jsonObject = new JSONObject(mValues);
                MyLogger.i(jsonObject.toString());
                String json = OkgoNetwork.post(url,jsonObject.toString());
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, DataBean.class));

            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<DataBean>() {
            @Override
            public void onNext(DataBean dataBean) {
                if (dataBean.getCode()==0){
                    MyToast.show("提交成功");
                    finish();
                }else {
                    MyToast.show(dataBean.getMsg());
                }
                dismissLoading();

            }
        });

    }


    //新
    private Map<Object,Object> mValues = new HashMap<>();
    private  String url = Constant.baseurl+"online/xsesdl/save";
    private LoadingViewDialog mLoadingView;
    //整合专项  基础 修改
    private void initBasic(){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Toolbar toolbar = (Toolbar)findViewById(com.oracle.children.R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mValues.put("token", User.getToken());
        mValues.put("jmId",getIntent().getIntExtra("jmid", 0) );
        mValues.put("userId", User.getUserId());//社区医生id;
        mValues.put("mark", 1);
        //随访id
        int id = getIntent().getIntExtra("id", 0);
        if (id !=0){
            mValues.put("sfjlId", id);
            mValues.put("id", id);
        }
        getParents();
    }

    private ParentsBean mParentsBean;

    private void getParents(){
        Observable.create(new ObservableOnSubscribe<ParentsBean>() {
            @Override
            public void subscribe(ObservableEmitter<ParentsBean> emitter) throws Exception {
                Map<Object, Object> values = new HashMap<>();
                values.put("grjbxxId",getIntent().getIntExtra("jmid", 0));
                values.put("mark", 1);
                values.put("token", User.getToken());
                JSONObject jsonObject = new JSONObject(values);
                MyLogger.i(jsonObject.toString());
                String json = OkgoNetwork.post(Constant.baseurl+"online/xse/checkFM",jsonObject.toString());
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, ParentsBean.class));
            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<ParentsBean>() {
            @Override
            public void onNext(ParentsBean parentsBean) {
                if (parentsBean.getData()!= null){
                    mParentsBean = parentsBean;
                    View[] views = ((ET36ArchivesExtendableListAdapter) expandableListView.getExpandableListAdapter()).getConvertViews();
                    View view = views[0];
                    if (mParentsBean !=null && mParentsBean.getData() !=null){
                        mValues.put("jbxxXseId", mParentsBean.getData().getId());
                        ((EditText) view.findViewById(R.id.threenewborn_fathername)).setText(mParentsBean.getData().getFxm());
                        ((EditText) view.findViewById(R.id.threenewborn_motherprofession)).setText(mParentsBean.getData().getMxm());
                        ((EditText) view.findViewById(R.id.threenewborn_fatherprofession)).setText(mParentsBean.getData().getFzy());
                        ((EditText) view.findViewById(R.id.threenewborn_mothername)).setText(mParentsBean.getData().getMzy());
                        ((EditText) view.findViewById(R.id.threenewborn_fatherphone)).setText(mParentsBean.getData().getFdh() );
                        ((EditText) view.findViewById(R.id.threenewborn_motherphone)).setText(mParentsBean.getData().getMdh());
                        ((TextView) view.findViewById(R.id.threenewborn_fatherbirthday)).setText(mParentsBean.getData().getFcsrq());
                        ((TextView) view.findViewById(R.id.threenewborn_motherbirthday)).setText(mParentsBean.getData().getMcsrq());
                    }

                }

            }
        });

    }



    public void showLoading(){
        mLoadingView = new LoadingViewDialog();
        mLoadingView.show(getSupportFragmentManager(),"" );
    }
    public void dismissLoading(){
        if (mLoadingView != null){
            mLoadingView.dismiss();
        }

    }


}
